unit uRoomControl;

interface
uses Classes,uRoomForm,SysUtils,SqlExpr,Forms,uAbstractControl,uRoomDM;

  type
    TRoomControl= class (TAbstractControl)
    class var FInstance:TRoomControl;
    constructor Create(AOwner:TComponent);
    destructor Destroy;override;
    procedure CreateDefaultForm;override;
    procedure CreateDefaultDM;override;
    function FindRoomName(RoomId: integer;Name:string):boolean;
    class function GetInstance:TRoomControl;
    strict private fRoomDM:TRoomDM;
    end;

implementation

{ TRoomControl }

constructor TRoomControl.Create(AOwner: TComponent);
begin
  fRoomDM:=nil;
  fRoomForm:=nil;
  inherited Create(AOwner);
end;


procedure TRoomControl.CreateDefaultDM;
begin
  inherited;
  fRoomDM:=TRoomDM.Create(Self);
  fRoomDM.DBConnection:=DBConnection;
end;

procedure TRoomControl.CreateDefaultForm;
begin
  inherited;
  if fRoomForm=nil then
  begin
    fRoomForm:=TRoomForm.Create(Self);
    fRoomForm.datControl.DataSet:=fRoomDM.cdsControl;
    fRoomForm.datControl.DataSet.Open;
  end
  else
    fRoomForm.Show;
end;

destructor TRoomControl.Destroy;
begin
  inherited;
end;

function TRoomControl.FindRoomName(RoomId: integer; Name: string): boolean;
  var
    sqlFind:TSQLQuery;
begin
  sqlFind:=TSQLQuery.Create(nil);
  try
    sqlFind.SQLConnection:=DBConnection;
    sqlFind.SQL.Add('SELECT * FROM ROOM');
    sqlFind.SQL.Add('  WHERE NAME= '+QuotedStr(Name));
    sqlFind.SQL.Add('  AND ROOM_ID <> '+IntToStr(RoomID));
    sqlFind.Open;

    result:=(sqlFind.RecordCount>0);
  finally
    sqlFind.Free;
  end;
end;

class function TRoomControl.GetInstance: TRoomControl;
begin
  if FInstance=nil then
  begin
    FInstance:=uRoomControl.TRoomControl.Create(Application);
  end;
  Result:=FInstance;
end;

end.
